package com.woniuxy.commons.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName FeignConfig
 * @Description openFeign配置类,拦截请求并在请求头上加上authorization(token信息)
 * @Author xianhao
 * @Date 2022/4/19 17:05
 * @Version 1.0.0
 */
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //获取到request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

       if(attributes != null){
           HttpServletRequest request = attributes.getRequest();
           //将token添加到头上
           template.header("authorization",request.getHeader("authorization"));
       }
    }
}
